package com.struggle.controller.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.struggle.common.ExchangeConstants;
import com.struggle.common.RoutingConstants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @className SendMessageController
 * @Description 发送消息
 * @Author liuwj3
 * @Date 2019/10/12 0012 上午 10:26
 **/
@RestController
@RequestMapping("/sendMessage")
public class SendMessageController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() throws Exception {
        String messageId = UUID.randomUUID().toString();
        String messageData = "刘文钊牛批：" + Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)).toString();
        String createDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        Map<String, Object> map = new HashMap<>(6);
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createDate", createDate);


        // 如果监听消息使用onMessage，且通过getBody拿取消息时需要如此设置
        /*Message message = MessageBuilder.withBody(new ObjectMapper().writeValueAsString(map).getBytes())
                .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .setContentEncoding("UTF-8")
                .setMessageId(UUID.randomUUID() + "")
                .build();*/

        // 发送消息到交换机，消息携带路由键DIRECT_ROUTE_KEY
        rabbitTemplate.convertAndSend(ExchangeConstants.DIRECT_EXCHANGE, RoutingConstants.DIRECT_ROUTE_KEY, map);
        rabbitTemplate.convertAndSend(ExchangeConstants.DIRECT_EXCHANGE, RoutingConstants.DIRECT_ROUTE_KEY2, "你好啊刘文钊");
        return new ObjectMapper().writeValueAsString(map);
    }

    @GetMapping("/sendTopicMessage")
    public String sendTopicMessage() {
        String messageId = UUID.randomUUID().toString();
        String messageData = "message: man";
        String createDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        Map<String, Object> map = new HashMap<>(6);
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createDate", createDate);

        rabbitTemplate.convertAndSend(ExchangeConstants.TOPIC_EXCHANGE, RoutingConstants.TOPIC_MAN, map);
        map.put("messageData", "message: woman");
        rabbitTemplate.convertAndSend(ExchangeConstants.TOPIC_EXCHANGE, RoutingConstants.TOPIC_WOMAN, map);
        return "ok";
    }

    @GetMapping("/sendFanoutMessage")
    public String sendFanoutMessage() {
        String messageId = UUID.randomUUID().toString();
        String messageData = "message: testFanoutMessage ";
        String createDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        Map<String, Object> map = new HashMap<>(6);
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createDate", createDate);

        rabbitTemplate.convertAndSend(ExchangeConstants.FANOUT_EXCHANGE, null, map);
        return "ok";
    }
}
